#include "roomdialog.h"
#include "ui_roomdialog.h"

roomDialog::roomDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::roomDialog)
{
    ui->setupUi(this);
    m_mainLayout = new QVBoxLayout;
    m_mainLayout->setContentsMargins(0,0,0,0);
    m_mainLayout->setSpacing(5);

    //设置一个竖直布局的画布，可以向其中添加控件
    ui->wdg_list->setLayout(m_mainLayout);

    ui->wdg_userShow->setMaximumHeight( 1000000 );
    //测试：
    /*for(int i = 1;i<=6;++i)
    {
        userShow* user1 = new userShow;
        user1->slot_setInfo(i,QString("测试%1").arg(i));
        slot_addUserShow(user1);
    }*/
}

roomDialog::~roomDialog()
{
    delete ui;
}

//设置整个房间页面的基本信息：房间号信息
void roomDialog::slot_setInfo(QString roomid)
{
    QString title = QString("房间号:%1").arg(roomid);
    setWindowTitle(title);
    ui->lb_title->setText(title);
}

//添加页面到房间窗口中
void roomDialog::slot_addUserShow(userShow *user)
{
    m_mainLayout->addWidget(user);
    m_mapToUsershow[user->m_id] = user;
}

//删除成员
void roomDialog::slot_removeUserShow(userShow *user)
{
    user->hide();
    m_mainLayout->removeWidget(user);
}

//清除传入id对应的usershow窗口
void roomDialog::slot_removeUserShow(int id)
{
    if(m_mapToUsershow.count(id) > 0)
    {
        userShow* user = m_mapToUsershow[id];
        slot_removeUserShow(user);
        m_mapToUsershow.erase(id);
        delete user;
    }
}

//清空所有usershow窗口
void roomDialog::slot_clearUserShow()
{
    for(auto it = m_mapToUsershow.begin();it!=m_mapToUsershow.end();++it)
    {
        slot_removeUserShow(it->second);
    }
}

//设置音频开关
void roomDialog::slot_setAudioCheck(bool check)
{
    ui->cb_audio->setChecked(check);
}

//设置视频开关
void roomDialog::slot_setVideoCheck(bool check)
{
    ui->cb_desk->setChecked(check);
}

//设置桌面采集开关
void roomDialog::slot_setScreenCheck(bool check)
{
    ui->cb_video->setChecked(check);
}

void roomDialog::slot_refreshUser(int id, QImage &img)
{
    //主页面的id与要刷新的图片的用户id一致，也要刷新主页面的预览图
    if(ui->wdg_userShow->m_id == id)
    {
        ui->wdg_userShow->slot_setImage(img);
    }
    if(m_mapToUsershow.count(id) > 0)
    {
        userShow* user = m_mapToUsershow[id];
        user->slot_setImage(img);
    }
}

//设置房间主显示页面的id和名字
void roomDialog::slot_setBigImgID(int id,QString name)
{
    ui->wdg_userShow->slot_setInfo(id,name);
}

//退出房间
void roomDialog::on_pb_close_clicked()
{
    this->close();
}

//退出房间
void roomDialog::on_pb_quit_clicked()
{
    this->close();
}

//关闭窗口，退出房间
void roomDialog::closeEvent(QCloseEvent *event)
{
    if(QMessageBox::question(this,"退出提示","是否退出会议？") == QMessageBox::Yes)
    {
        Q_EMIT SIG_close();
        event->accept();
        return;
    }
    event->ignore();
}

//开启和关闭音频
void roomDialog::on_cb_audio_clicked()
{
    if(ui->cb_audio->isChecked())
    {
        Q_EMIT SIG_audioStart();
    }
    else
    {
        Q_EMIT SIG_audioPause();
    }
}

//开启或关闭视频
void roomDialog::on_cb_video_clicked()
{
    if(ui->cb_video->isChecked())
    {
        ui->cb_desk->setChecked(false);
        Q_EMIT SIG_screenPause();
        Q_EMIT SIG_videoStart();
    }
    else
    {
        Q_EMIT SIG_videoPause();
    }
}

//开启或关闭桌面
void roomDialog::on_cb_desk_clicked()
{
    if(ui->cb_desk->isChecked())
    {
        ui->cb_video->setChecked(false);
        Q_EMIT SIG_videoPause();
        Q_EMIT SIG_screenStart();
    }
    else
    {
        Q_EMIT SIG_screenPause();
    }
}

//在房间页面改变了滤镜设置
void roomDialog::on_cb_moji_currentIndexChanged(int index)
{
    Q_EMIT SIG_setMoji(index);
}

